<?php
namespace App\Service;

use App\Models\User\User;
use App\Models\User\WebUser;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use EasyWeChat\Kernel\Messages\Image;

class WechatService {
    protected  $app;
    protected $opendId;
    public function __construct()
    {
        $this->app =  \EasyWeChat::officialAccount(); // 公众号
//        $this->opendId = $this->wechatUser()->getId();
    }
    public function wechatUser()
    {
        $user = session('wechat.oauth_user.default'); // 拿到授权用户资料
        return $user;
    }

    public function getWechatInfoByOpendid($openId)
    {
        $user = $this->app->user->get($openId);
        session(['wechat.oauth_user.default'=>$user]);

        return $user;
    }

    public function user()
    {
        $user = User::where('openid',$this->opendId)->first();
        return $user;
    }

    public function updateUserInfo()
    {
        dump('我进入了');
        $user = User::query()->where('openid',$this->opendId)->first();
        dump('systemuser',$user);
        $userInfo  = $this->getWechatInfoByOpendid($this->opendId);
        dd($userInfo);
    }

    public static function test()
    {
        dd(23);
    }


    public function EventMessage($message)
    {
        Log::info('EventMessage',$message);
        $openid = $message['FromUserName'];
        switch ($message['Event']) {
            case 'subscribe':
                Log::info('switch-subscribe',$message);
                $this->subscribe($openid);
                return '欢迎，关注 顾德-阿福特卤';
                break;
            case 'unsubscribe':
                $this->unsubscribe($openid);
                break;
                return '欢迎，关注 顾德-阿福特卤';
            case 'CLICK':
                $EventKey = $message['EventKey'];
                return $this->click($EventKey);
            default:
                return '顾德-阿福特卤 很高兴为你服务';
                break;
        }

    }

    public function unsubscribe($openid)
    {
        $user = WebUser::query()->where([
            'openid' => $openid,
        ])->delete();
    }
    public function subscribe($openid){
        Log::info('subscribe',[$openid]);
        $user = WebUser::query()->where([
            'openid' => $openid,
        ])->first();

        if (!$user) {

           $user =  new WebUser(['openid' => $openid]);
           $user->save();
            Log::info('user create',[$user]);
        } else {
            Log::info('user exists',[$user]);
        }

    }

    public function menu()
    {
        $button = config('wechat.menu');
        $buttons = [
            [
                "type" => "click",
                "name" => "今日歌曲",
                "key"  => "V1001_TODAY_MUSIC"
            ],
            [
                "name"       => "菜单",
                "sub_button" => [
                    [
                        "type" => "view",
                        "name" => "搜索",
                        "url"  => "http://www.soso.com/"
                    ],
                    [
                        "type" => "view",
                        "name" => "视频",
                        "url"  => "http://v.qq.com/"
                    ],
                    [
                        "type" => "click",
                        "name" => "赞一下我们",
                        "key" => "V1001_GOOD"
                    ],
                ],
            ],
        ];
        $data = $this->app->menu->create($button);
        dd($data,$buttons,$button);

    }

    # $app->material->list($type, $offset, $count);
    public function material()
    {
        $material = $this->app->material->list('image', 0, 10);
        dd($material);
    }

    public function click($EventKey)
    {
        switch ($EventKey) {
            case 'B5_click':
                $image = new Image("BlASBDYi9G8qFhD2SKwMV-HHZOHv_j1_865blX0RLEs");
                return $image;
            default:
                return '顾德-阿福特卤 很高兴为你服务';
                break;
        }
    }


}
